<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/24
 * Time: 17:01
 */

namespace app\api\controller\v1;

use app\common\lib\exception\ApiException;
use think\Db;
use app\api\model\Information as M;

class Information extends Common
{


    /**
     * 获取用户所有消息
     * @param information_id int 消息id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function index(){
        $result = (new M())->allInformation();
        return $result;
    }
    /**
     * 获取未查看的消息记录
     * @param information_id int 消息id *必传
     * @param information_status int 消息状态 0未查看 1已查看 默认为0
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function no_read_information_list(){
        $result = (new M())->noSeeInformation();
        return $result;
    }
    /**
     * 查看所有未查看的消息
     * @param information_id int 消息id *必传
     * @param information_status int 消息状态 0未查看 1已查看
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function edit(){
        $m = new M();
        $result = $m->seeAllInformation();
        return $result;
    }

    /**
     * 清空用户所有消息
     * @param user_id int 用户id *必传
     * @param information_status int 消息状态 0未查看 1已查看
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function emptyAll(){
        if(request()->isPost()){
            $data = $_POST;
            $information = new M();
            $info = db('information')->where('user_id='.$data['information_id'])->field('id')->select();

            if(empty($info)){
                return show(config('code.error'),'暂无消息!');
            }

            $res = $information->where('user_id="'.$data['information_id'].'"')->delete();

            if($res){
                return show(config('code.success'),'清空消息成功!');
            }else{
                return show(config('code.error'),'清空消息失败!');
            }
        }else{
            return show(config('code.error'),'请求不合法!');
        }
    }
}